﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BattleThis.Core;
using System.Web.Security;
using BattleThis.Mvc;
using BattleThis.Common;

namespace BattleThis.Controllers
{

    public class GameController : InGameControllerAbstract
    {

		#region Variablen
		public const string IndexAction = "Index";
		public const string SwitchUserAction = "SwitchUser";
		public const string FurtherAction = "Further";
		public const string MoveAction = "Move";
		public const string SelectAction = "Select";

		public static readonly ReflectedControllerDescriptor Info = new ReflectedControllerDescriptor( typeof( GameController ) );
		#endregion

		#region Properties

		#endregion

		#region Methoden

		[ActionName( IndexAction )]
		public ActionResult Index()
		{
			this.GameContext.Selection = Selection.None;

			return View( "Index", this.Game );
		}

		[ActionName( MoveAction )]
		public ActionResult MoveUnit( int unitId, int column, int row )
		{
			if( !this.Game.Map.IsValidId( unitId ) ||
				!this.Game.CurrentPlayer.HasUnit( unitId ) )
			{
				return RedirectToAction( "Index" );
			}

			var selected = this.Game.Map.Find( unitId );

			this.Game.Movement.Move( selected, new Position( column, row ) );

			return RedirectToAction( "Index" );
		}

		[ActionName( SelectAction )]
		public ActionResult SelectUnit( int unitId )
		{
			if( !this.Game.Map.IsValidId( unitId ) ) return Index();

			var location = this.Game.Map.Find( unitId );

			this.HttpContext.GetGameContext().Selection = CreateSelection( location );
			this.Widgets.Add( new SelectionWidget( this.GameContext ) );

			return View( "Index", this.Game );
		}

		[ActionName( SwitchUserAction )]
		public ActionResult Switch()
		{
			FormsAuthentication.SignOut();

			if( this.HttpContext.User.Identity.Name == this.Game.Players.First().Name )
			{
				this.HttpContext.User = this.Game.Players.Last();
			}
			else
			{
				this.HttpContext.User = this.Game.Players.First();
			}

			//this.Session.Abandon();
			FormsAuthentication.SetAuthCookie( this.HttpContext.User.Identity.Name, false );

			return RedirectToAction( "Index" );
		}

		[ActionName( FurtherAction )]
		public ActionResult Further()
		{
			var requester = HttpContext.User;

			if( this.Game.IsFinished() )
			{
				var service = new GameService();
				service.Close( this.Game );

				return RedirectToAction( LobbyController.IndexAction, LobbyController.Info.ControllerName );
			}

			if( this.Game.CurrentPlayer.Name.Equals( HttpContext.User.Identity.Name ) )
			{
				this.Game.NextPlayer();
			}

			return Switch();
		}

		/// <summary>
		/// Creates the selection-object.
		/// </summary>
		/// <param name="location">The location.</param>
		/// <returns></returns>
		protected Selection CreateSelection( Field location)
		{
			return new Selection( location.Unit,
									this.Game.Movement.GetRange( location.Unit ),
									this.Game.Battle.GetRange( location.Unit, this.Game.CurrentPlayer ) );
		}

		#endregion

    }
}
